
	***************
	** Prepare figure 1, panel A 
	***************
	
	** gdp per capita - ppp
		insheet using "$dir/rawdata/GDP/API_NY.GDP.PCAP.PP.CD_DS2_en_csv_v2_5607096.csv", names clear
		ren datasource country
		ren worlddevelopmentindicators country_code
		ren v65 gdppercapppp_2020
		drop if _n<=2
		keep country country_code gdppercapppp_2020
		tempfile gdppercapppp_2020
		save `gdppercapppp_2020', replace

	** Merge population projections with GDP figures
		use "$dir/data/population_projections_selectcountries.dta", clear
		ren countryregion country
		merge 1:1 country using `gdppercapppp_2020', nogen
					
	** Keep countries/regions for table
		gen keep=1 if country=="Ethiopia" | country=="Ghana" | country=="Malawi" | country=="Nigeria" | country=="South Africa" | country=="Tanzania" | country=="Uganda" | country=="Niger"
		replace keep=1 if country=="Rest of world" | country=="Sub-Saharan Africa" | country=="World"
		keep if keep==1
		drop keep
		gen iscountry=inlist(country,"Ethiopia","Ghana","Malawi","Nigeria","South Africa","Tanzania","Uganda","Niger")
		gsort -iscountry gdppercapppp_2020			
		order country country_code iscountry gdppercapppp_2020
		
	** Share of population over 60
		gen popshare_60plus_1990_mod=pop_60plus_1990/pop_total_1990
		gen popshare_60plus_2020_mod=pop_60plus_2020/pop_total_2020
		gen popshare_60plus_2050_mod=pop_60plus_2050/pop_total_2050
		gen popshare_60plus_2100_mod=pop_60plus_2100/pop_total_2100
				
	** Rest of world GDP
	
			sum gdppercapppp_2020 if country=="Sub-Saharan Africa"
			local gdppercappppssa=`r(mean)'
			sum pop_total_2020 if country=="Sub-Saharan Africa"
			local popssa=`r(mean)'
			
			sum gdppercapppp_2020 if country=="World"
			local gdppercappppworld=`r(mean)'
			sum pop_total_2020 if country=="World"
			local popworld=`r(mean)'
			
			sum pop_total_2020 if country=="Rest of world"
			local poprow=`r(mean)'
			
			local gdppercapppprow=(`gdppercappppworld'*`popworld'-`gdppercappppssa'*`popssa')/`poprow'
			di `gdppercapppprow'
			replace gdppercapppp_2020=`gdppercapppprow' if country=="Rest of world"
			
	** Dependency ratios
	
		foreach year of numlist 1990 2020 2050 2100 {
			
			foreach var in depratio_15to59 depratio_15to64 oldagedepratio_15to59 oldagedepratio_15to64 {
				
				sum `var'_`year' if country=="Sub-Saharan Africa"
				local ssamean=`r(mean)'
				sum pop_total_`year' if country=="Sub-Saharan Africa"
				local popssa=`r(mean)'
				
				sum `var'_`year' if country=="World"
				local worldmean=`r(mean)'
				sum pop_total_`year' if country=="World"
				local popworld=`r(mean)'
				
				sum pop_total_`year' if country=="Rest of world"
				local poprow=`r(mean)'
				
				local varrow=(`worldmean'*`popworld'-`ssamean'*`popssa')/`poprow'
	
				replace `var'_`year'=`varrow' if country=="Rest of world"
			
			}
		
		}
			
		order country country_code gdppercapppp_2020	
	
	***************
	***************
	
	** Clean up and produce table
				
		order country gdppercapppp_2020 pop_total_2020 pop_60plus_1990 pop_60plus_2020 pop_60plus_2050 growth_pop_60plus_2020to2050
		foreach var of varlist pop_total_* pop_60plus_* pop_65plus_* pop_70plus_* pop_75plus_* pop_below15_* pop_15to59_* pop_15to64_* {
			replace `var'=`var'/1000
		}
		
		replace gdppercapppp_2020=round(gdppercapppp_2020,1.0)
		format gdppercapppp_2020 %15.0gc
		
		gen gdppercapppp_2020_string=string(gdppercapppp_2020)
		
		foreach var in pop_total_2020 pop_60plus_1990 pop_60plus_2020 pop_60plus_2050 {
			replace `var'=round(`var',0.1) if `var'<1
			replace `var'=round(`var',1.0) if `var'>=1
			format `var' %15.1fc
			gen `var'_string=string(`var')
			replace `var'_string="0"+`var'_string if `var'<1
			
		}
		
		replace growth_pop_60plus_2020to2050=round(growth_pop_60plus_2020to2050,1.0)
		gen growth_pop_60plus_2020to2050_str=string(growth_pop_60plus_2020to2050)+"%"
		
		foreach var in gdppercapppp_2020 pop_total_2020 pop_60plus_1990 pop_60plus_2020 pop_60plus_2050 {
			replace `var'_string=substr(`var'_string,1,2)+","+substr(`var'_string,3,3) if strlen(`var'_string)==5
			replace `var'_string=substr(`var'_string,1,1)+","+substr(`var'_string,2,3) if strlen(`var'_string)==4
		}
		
		keep country gdppercapppp_2020_string pop_total_2020_string pop_60plus_1990_string pop_60plus_2020_string pop_60plus_2050_string growth_pop_60plus_2020to2050_str
		order country gdppercapppp_2020_string pop_total_2020_string pop_60plus_1990_string pop_60plus_2020_string pop_60plus_2050_string growth_pop_60plus_2020to2050_str
		ren *_string *
		ren *_str *
		ren growth_pop_60plus_2020to2050 gr_pop60plus_2020to2050
		drop if country=="World"
		texsave * using "$dir/output/fig1a.tex", frag replace
